package com.cgbl.service.vm

import android.app.Application
import api.WorkServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.TeamListBean
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import com.robot.base.model.BaseResponse
import com.robot.base.model.TagListBean
import com.robot.base.model.UserInfoBean
import com.robot.base.util.LiveDataEventManager

class MemberSearchViewModel(application: Application) : GLBaseViewModel(application) {

    val toAddTeamCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Project.A_ADD_MEMBER)
    })

    val uc = UIChangeEvent()

    class UIChangeEvent {

        val getUserInfoDataEvent = SingleLiveEvent<List<UserInfoBean>>()

    }

    fun fetchMemberList(keyword: String) {
        WorkServiceFactory.getOtherUsers(keyword).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<List<UserInfoBean>>>(this) {
            override fun onNext(t: BaseResponse<List<UserInfoBean>>) {
                uc.getUserInfoDataEvent.postValue(t.data)
            }
        })
    }


    fun addMember(id: String) {
        val params = mapOf("accountId" to id)
        WorkServiceFactory.createAccount(params)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
                    LiveDataEventManager.postRefreshMemberEvent()
                    finish()
                }
            })
    }

}